/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.feature.solr.local.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

/**
 * SolrServer工厂Bean
 *
 * @see SolrServerFactory
 * @author yoara
 */
public class SolrServerFactoryBean implements FactoryBean<SolrServerFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
	private final static Logger logger = LoggerFactory.getLogger(SolrServerFactoryBean.class);
	private SolrServerFactory solrServerFactory;

	public void onApplicationEvent(ApplicationEvent event) {
	}

	public void afterPropertiesSet() throws Exception {
		solrServerFactory = new SolrServerFactory();
		logger.info("******** SolrServerFactory init finished ***********");
	}

	public SolrServerFactory getObject(){
		if (solrServerFactory == null) {
			try{
				afterPropertiesSet();
			}catch (Exception e){}
		}
		return this.solrServerFactory;
	}

	public Class<?> getObjectType() {
		return this.solrServerFactory == null ? SolrServerFactory.class : this.solrServerFactory.getClass();
	}

	public boolean isSingleton() {
		return true;
	}
}
